﻿using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace Inkubator.Wizard.WebWizard.WizardControl
{
    public class ControlRepresentation
    {
        /// <summary>
        /// Receive a List of WebControls and convert the user input in
        /// each control to an object to be procesed later.
        /// </summary>
        /// <param name="controls"></param>
        /// <returns></returns>
        public static object[] parseControlList(List<WebControl> controls)
        {
            List<object> result = new List<object>();

            foreach (WebControl control in controls)
            {
                result.Add( ControlRepresentation.controlValueToString(control) );
            }

            return result.ToArray();
        }

        /// <summary>
        /// Convert the value (user input) from a WebControl to an object that
        /// can be procesed directly in any function.
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public static object controlValueToString(WebControl control)
        {
            string q = control.GetType().ToString();
            if (control.GetType() == typeof(TextBox))
            {
                if (((TextBox)control).Text != null)
                {
                    return ((TextBox)control).Text;
                }
            }
            else if (control.GetType() == typeof(Calendar))
            {
                return ((Calendar)control).SelectedDate;
            }
            else if (control.GetType() == typeof(ListBox) ||
                control.GetType() == typeof(CheckBoxList) ||
                control.GetType() == typeof(DropDownList) ||
                control.GetType() == typeof(RadioButtonList))
            {
                return ((ListControl)control).SelectedValue;
            }
            else if (control.GetType() == typeof(Table))
            {
                return ((Table)control).Rows;
            }

            return null;
        }

    }
}
